#!/bin/bash
# tmux
timedatectl set-timezone Asia/Shanghai
rm ~/install.lock
echo `date +"%Y-%m-%d %H:%M:%S" ` 'start' >> ~/install.lock
apt update
# apt install -y unzip libaio1 numactl libtinfo5
# rm mysql*
# #wget http://hx.phasercn.com/download/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
# #wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
# wget https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
# #wget https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
# tar -Jxf mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
# mv ~/mysql-8.0.31-linux-glibc2.12-x86_64 /usr/local
# cd /usr/local
# mv mysql-8.0.31-linux-glibc2.12-x86_64 mysql
# groupadd mysql && useradd -r -g mysql mysql
# mkdir -p /data/mysql
# chown mysql:mysql -R /data/mysql
# echo '[mysqld]
# bind-address=0.0.0.0
# port=3306
# user=mysql
# basedir=/usr/local/mysql
# datadir=/data/mysql
# socket=/tmp/mysql.sock
# log-error=/data/mysql/mysql.err
# pid-file=/data/mysql/mysql.pid
# # character config
# character_set_server=utf8mb4
# symbolic-links=0
# explicit_defaults_for_timestamp=true
# [client]
# port=3306
# default-character-set=utf8
# socket=/tmp/mysql.sock' >/etc/my.cnf
# cd /usr/local/mysql/bin
# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize-insecure
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
# /etc/init.d/mysql.server start
# ./mysqladmin -S /tmp/mysql.sock -u root password ""
# update-rc.d mysql.server defaults
# apt install -y default-mysql-server default-mysql-client

#args_ip=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')

#args_pwd='wowmanage.cn'
#args_name='AzerothCore'
argsType=$1
argsVer=$2

# 开放3306端口
# ufw enable
ufw allow 22
ufw allow 3306
ufw allow 8085
ufw allow 3724
ufw allow 7878
ufw reload

apt install -y unzip

echo `date +"%Y-%m-%d %H:%M:%S" ` 'download' >> ~/install.lock
rm -rf /azerothcore
mkdir /azerothcore
cd /azerothcore

# wget http://hx.phasercn.com/download/ac-1-20230320.zip
# wget http://hx.phasercn.com/download/ac-2-20230328.zip
wget --no-check-certificate --timeout=10 --tries=2 "http://hx.phasercn.com/download/ac-$argsType-$argsVer.zip"
unzip -q "ac-$argsType-$argsVer.zip"


echo `date +"%Y-%m-%d %H:%M:%S" ` 'setEnv' >> ~/install.lock
#sudo apt-get update && sudo apt-get install -y git cmake make gcc g++ clang libmysqlclient-dev libssl-dev libbz2-dev libreadline-dev libncurses-dev libboost-all-dev mysql-server tmux unzip
# apt install -y libmysqlclient-dev libboost-all-dev unzip
# if [ "$argsType" = 1 ]; then
#     apt install -y libace-dev
# fi
bash /azerothcore/apps/installer/main.sh install-deps
# apt install -y default-mysql-client

# 设置允许远程登录
echo '[mysqld]' >>  /etc/mysql/my.cnf
echo 'bind-address=0.0.0.0' >>  /etc/mysql/my.cnf
echo 'default_authentication_plugin=mysql_native_password' >>  /etc/mysql/my.cnf
service mysql restart


echo `date +"%Y-%m-%d %H:%M:%S" ` 'initData' >> ~/install.lock

# CREATE USER 'root'@'%' IDENTIFIED BY 'wowmanage.cn' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;

mysql -uroot <<EOF
DROP USER IF EXISTS 'root'@'%';
CREATE USER 'root'@'%' IDENTIFIED BY 'wowmanage.cn' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;
GRANT ALL PRIVILEGES ON * . * TO 'root'@'%';
DROP USER IF EXISTS 'acore'@'localhost';
CREATE USER 'acore'@'localhost' IDENTIFIED BY 'acore' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;
GRANT ALL PRIVILEGES ON * . * TO 'acore'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
#cp /azerothcore/apps/db_assembler/conf.dist.sh /azerothcore/apps/db_assembler/config.sh
chmod +x /azerothcore/apps/db_assembler/db_assembler.sh
if [ "$argsType" = 1 ]; then
    echo "import-all"
    bash "/azerothcore/apps/db_assembler/db_assembler.sh" import-all
else
    echo "import-bases"
    bash "/azerothcore/apps/db_assembler/db_assembler.sh" import-bases
fi



# 下载data
cd ~
rm /root/data.zip
if [ "$argsType" = 1 ]; then
    wget http://hx.phasercn.com/client-data/releases/download/v0/data.zip
else
    wget http://hx.phasercn.com/client-data/releases/download/v16/data.zip
fi
cd /azerothcore/env/dist/bin
unzip -q -o ~/data.zip

#复制配置文件
cp /azerothcore/env/dist/etc/authserver.conf.dist /azerothcore/env/dist/etc/authserver.conf
cp /azerothcore/env/dist/etc/worldserver.conf.dist /azerothcore/env/dist/etc/worldserver.conf

echo `date +"%Y-%m-%d %H:%M:%S" ` 'success' >> ~/install.lock


# bash install.sh 0 20230318
# bash install.sh 0 20230328
# bash install.sh 1 20230320
# bash install.sh 2 20230318
# bash install.sh 2 20230328
